﻿using System.IO;
using System.Reflection;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Installer;
using System;

[assembly: CLSCompliantAttribute(true)]

namespace Shape.Mvp
{
   public class Application : IDisposable
   {
      private IWindsorContainer _container;
      private IApplicationController _applicationController;

      public Application()
      {
         _container = 
            new WindsorContainer().
               Install(FromAssembly.InDirectory(new AssemblyFilter(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))));

         _applicationController = _container.Resolve<IApplicationController>();
      }

      public IApplicationController Controller
      {
         get { return _applicationController; }
      }


      public void Dispose()
      {
         Dispose(true);
         GC.SuppressFinalize(this);
      }

      protected virtual void Dispose(bool disposing)
      {
         if (disposing)
         {
            // free managed resources
            if (_container != null)
            {
               if (_applicationController != null)
               {
                  _container.Release(_applicationController);
               }

               _container.Dispose();
               _container = null;
            }
         }
      }
   }
}
